<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="chrome=1">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" type="text/css">
    <link rel="stylesheet" href="../css/ol.css" type="text/css">
    <link rel="stylesheet" href="./resources/layout.css" type="text/css">
    <style>
      body {
        padding-top: 70px;
      }
      input.search-query {
        color: #333;
      }
      .example {
        display: block;
        padding: 10px;
        background-color: #F5F5F5;
        height: 140px;
        margin: 10px 0;
        overflow: auto;
      }
      .example p.description {
        font-size: smaller;
        margin: 5px 0;
      }
      .example:hover {
        background-color: #ddd;
      }

      ::-webkit-scrollbar {
        width: 8px;
      }

      ::-webkit-scrollbar-button {
        width: 0;
        height: 0;
        display: none;
      }

      ::-webkit-scrollbar-thumb {
        background-color: rgba(0, 0, 0, 0.2);
        -webkit-box-shadow: inset 1px 1px 0 rgba(0, 0, 0, 0.10),inset 0 -1px 0 rgba(0, 0, 0, 0.07);
      }

      ::-webkit-scrollbar-thumb:hover {
        background-color: rgba(0, 0, 0, 0.4);
      }

      ::-webkit-scrollbar-corner {
        background-color: transparent;
      }
    </style>
    <script type="text/javascript" src="Jugl.js"></script>
    <script type="text/javascript" src="index.js"></script>
    <script type="text/javascript">
      var template, target;

      function listExamples(examples) {
        target.innerHTML = "";
        var node = template.process({
          context: {examples: examples},
          clone: true,
          parent: target
        });
        document.getElementById("count").innerHTML = "(" + examples.length + ")";
      }

      var timerId;
      function inputChange() {
        if (timerId) {
          window.clearTimeout(timerId);
        }
        var text = this.value;
        timerId = window.setTimeout(function() {
          filterList(text);
        }, 500);
      }

      function filterList(text) {
        var examples;
        if (text.length < 2) {
          examples = info.examples;
        } else {
          var words = text.split(/\W+/);
          var scores = {};
          for(var i=0; i<words.length; ++i) {
            var word = words[i].toLowerCase();
            var dict = info.index[word];
            var updateScores = function() {
              for(exIndex in dict) {
                var count = dict[exIndex];
                if(scores[exIndex]) {
                  if(scores[exIndex][word]) {
                    scores[exIndex][word] += count;
                  } else {
                    scores[exIndex][word] = count;
                  }
                } else {
                  scores[exIndex] = {};
                  scores[exIndex][word] = count;
                }
              }
            };
            if (dict) {
              updateScores();
            } else {
              var r;
              for (idx in info.index) {
                r = new RegExp(word);
                if (r.test(idx)) {
                  dict = info.index[idx];
                  updateScores();
                }
              }
            }
          }
          examples = [];
          for (var j in scores) {
            var ex = info.examples[j];
            ex.score = scores[j];
            examples.push(ex);
          }
          // sort examples by first by number of words matched, then
          // by word frequency
          examples.sort(function(a, b) {
            var cmp;
            var aWords = 0, bWords = 0;
            var aScore = 0, bScore = 0;
            for (var i in a.score) {
              aScore += a.score[i];
              aWords += 1;
            }
            for (var j in b.score) {
              bScore += b.score[j];
              bWords += 1;
            }
            if (aWords == bWords) {
              cmp = bScore - aScore;
            } else {
              cmp = bWords - aWords;
            }
            return cmp;
          });
        }
        listExamples(examples);
      }

      function parseQuery() {
        var params = {};
        var list = window.location.search.substring(1).split("&");
        for (var i = 0; i < list.length; ++i) {
          var pair = list[i].split("=");
          if (pair.length == 2) {
            params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
          }
        }
        if (params["q"]) {
          var input = document.getElementById("keywords");
          input.value = params["q"];
          inputChange.call(input);
        }
      }

      window.onload = function() {
        for (var i = 0; i < info.examples.length; ++i) {
          info.examples[i].link += window.location.search;
        }
        template = new jugl.Template("template");
        target = document.getElementById("examples");
        listExamples(info.examples);
        document.getElementById("keywords").onkeyup = inputChange;
        parseQuery();
      };

    </script>

    <title>OpenLayers Examples</title>
  </head>
  <body>

    <header class="navbar navbar-fixed-top" role="navigation">
      <div class="container">
        <div class="display-table pull-left">
          <a class="navbar-brand" href="./"><img src="./resources/logo-70x70.png">&nbsp;OpenLayers Examples</a>
          <form class="navbar-form" role="search">
            <input name="q" type="text" id="keywords" class="search-query" placeholder="Search" autofocus>
            <span id="count"></span>
          </form>
        </div>
        <!-- menu items that get hidden below 768px width -->
        <nav class='collapse navbar-collapse navbar-responsive-collapse'>
          <ul class="nav navbar-nav pull-right">
            <li><a href="../doc">Docs</a></li>
            <li><a class="active" href="index.html">Examples</a></li>
            <li><a href="../apidoc">API</a></li>
            <li><a href="https://github.com/openlayers/openlayers">Code</a></li>
          </ul>
        </nav>
      </div>
    </header>

    <div class="container-fluid">
      <div id="examples"></div>
      <div style="display: none;">
        <div id="template" class="row">
          <div class="col-md-4 col-sm-4" jugl:repeat="example examples">
            <a jugl:attributes="href example.link" class="example">
              <span class="mainlink">
                <strong><span jugl:replace="example.title">title</span></strong><br>
                <small jugl:content="'(' + example.example + ')'"></small>
              </span>
              <p class="description" jugl:content="example.shortdesc"></p>
            </a>
          </div>
        </div>
      </div>
    </div>
  </body>
</html>
